Skip to content

fix(main): navigate to domain before cookie/header strategy commands in CDP mode#71

Merged
jackwener merged 1 commit intojackwener:mainfrom
backtime1993:fix/cookie-cdp-domain-nav-v2
Mar 18, 2026
Merged

fix(main): navigate to domain before cookie/header strategy commands in CDP mode#71
jackwener merged 1 commit intojackwener:mainfrom
backtime1993:fix/cookie-cdp-domain-nav-v2

Conversation

@backtime1993
Copy link
Contributor

Summary

Cookie/header strategy commands fail with Failed to fetch when using CDP mode (OPENCLI_CDP_ENDPOINT) and the active Chrome tab is on an unrelated domain.

Root cause: fetch() with credentials: 'include' runs in the context of the current page. In CDP mode, this is the user's active tab (e.g. YouTube), not the target domain (e.g. bilibili.com). The browser blocks cross-origin credentialed requests per same-origin policy.

Fix: Navigate to cmd.domain before executing cookie/header strategy commands, mirroring the pre-navigation already done in the cascade command (main.ts line ~155).

Repro

# Enable CDP mode
export OPENCLI_CDP_ENDPOINT=1

# Have any non-bilibili tab active in Chrome
opencli bilibili search --keyword "test"
# → Error: Failed to fetch

Changes

  • src/main.ts: Add domain pre-navigation for Strategy.COOKIE and Strategy.HEADER commands
  • Import Strategy enum from registry

Test plan

  • bilibili search works regardless of active Chrome tab (CDP mode)
  • bilibili hot (public strategy) unaffected
  • youtube search (cookie strategy) works
  • Extension mode unaffected (new tab starts as about:blank)

🤖 Generated with Claude Code

…in CDP mode

When using CDP mode (OPENCLI_CDP_ENDPOINT), the browser page context is
the user's active tab which may be on an unrelated domain. Cookie/header
strategy commands that use fetch() with credentials: 'include' then fail
with "Failed to fetch" due to the browser's same-origin policy.

Fix: before executing cookie/header strategy commands, navigate to the
command's declared domain so the fetch runs in same-origin context.
This mirrors the pre-navigation already done in the cascade command.

Affects all cookie-strategy adapters (bilibili, twitter, zhihu, xueqiu,
etc.) when OPENCLI_CDP_ENDPOINT is enabled and the active Chrome tab is
on a different site.
@jackwener jackwener merged commit a4f9491 into jackwener:main Mar 18, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants